﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebMatrix.WebData;
using Mvc4.Filters;
using Mvc4.Models;
using System.IO;
using BussinesLayer.Services;
using DataAccess.Datatypes;
using System.Web.Helpers;


namespace Mvc4.Controllers
{
    [Authorize]
    [InitializeSimpleMembership]
    public class GoodController : Controller
    {
        private GoodServices gs = new GoodServices();

        //
        // GET: /Good/

        public ActionResult Index(int siteId, string siteName)
        {
            ViewBag.siteId = siteId;
            ViewBag.Name = siteName;
            ViewBag.SiteName = siteName + " - My Stuff";
            return View(gs.GetUserGoods(siteId, WebSecurity.CurrentUserId));
        }

        //
        // GET: /Good/Details/5

        [AllowAnonymous]
        public ActionResult Details(int siteId, string siteName, int id = 0, string error = "")
        {
            ViewBag.siteId = siteId;
            ViewBag.Name = siteName;
            ViewBag.goodId = id;
            ViewBag.Error = error != "";

            Good good = gs.GetGood(id, WebSecurity.CurrentUserId);
            if (good == null)
            {
                return HttpNotFound();
            }

            ViewBag.SiteName = siteName + " - " + good.Name;

            return View(good);
        }

        [Authorize]
        [InitializeSimpleMembership]
        public ActionResult AddContent(int goodId, string newUrl, string newComment)
        {
            int userId = WebSecurity.CurrentUserId;
            gs.AddContent(goodId, newUrl, newComment, DateTime.Now, userId);
            return Redirect(Request.UrlReferrer.AbsolutePath);
        }

        public ActionResult AddLike(string siteName, int goodId)
        {
            gs.AddLike(goodId, WebSecurity.CurrentUserId);
            return Redirect(Request.UrlReferrer.AbsolutePath);
        }

        public ActionResult RemoveLike(int goodId)
        {
            gs.RemoveLike(goodId, WebSecurity.CurrentUserId);
            return Redirect(Request.UrlReferrer.AbsolutePath);
        }

        public ActionResult AddInap(int contentId)
        {
            gs.AddInnapropiate(contentId, WebSecurity.CurrentUserId);
            return Redirect(Request.UrlReferrer.AbsolutePath);
        }

        //
        // GET: /Good/Create

        public ActionResult Create(int siteId, string siteName)
        {
            ViewBag.siteId = siteId;
            ViewBag.Name = siteName;
            ViewBag.SiteName = siteName + " - New Good";
            return View();
        }

        //
        // POST: /Good/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(GoodSetFiles goodset, int siteId, string siteName)
        {
            ViewBag.siteId = siteId;
            ViewBag.Name = siteName;
            ViewBag.SiteName = siteName + " - New Good";
            byte[] image = null;
            byte[] bigImage = null;

            if (ModelState.IsValid)
            {
                //Si se subieron fotos
                if (goodset.File1 != null)
                {
                    MemoryStream target = new MemoryStream();
                    goodset.File1.InputStream.CopyTo(target);
                    image = target.ToArray();
                }
                if (goodset.File2 != null)
                {
                    MemoryStream target = new MemoryStream();
                    goodset.File2.InputStream.CopyTo(target);
                    bigImage = target.ToArray();
                }

                gs.AddGood(siteId, WebSecurity.CurrentUserId, goodset.Name, image, bigImage, goodset.PriceDay, goodset.Latitude, goodset.Longitude);
                return RedirectToAction("MyStuff", siteName, null);
            }

            return View(goodset);
        }

        //
        // GET: /Good/Edit/5

        public ActionResult Edit(int siteId, string siteName, int id = 0)
        {
            ViewBag.siteId = siteId;
            ViewBag.Name = siteName;
            Good good = gs.GetGoodEdit(id, WebSecurity.CurrentUserId, siteId);

            if (good == null)
            {
                return HttpNotFound();
            }

            ViewBag.SiteName = siteName + " - " + good.Name;

            GoodSetFiles goodset = new GoodSetFiles();
            goodset.id = good.Id;
            goodset.Name = good.Name;
            goodset.PriceDay = good.PriceDay;
            goodset.Latitude = good.Latitude;
            goodset.Longitude = good.Longitude;

            return View(goodset);
        }

        //
        // POST: /Good/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(GoodSetFiles goodset, int siteId, string siteName)
        {
            ViewBag.siteId = siteId;
            ViewBag.Name = siteName;
            ViewBag.SiteName = siteName + " - " + goodset.Name;

            if (ModelState.IsValid)
            {
                byte[] image = null;
                byte[] bigImage = null;

                if (goodset.File1 != null)
                {
                    MemoryStream target = new MemoryStream();
                    goodset.File1.InputStream.CopyTo(target);
                    image = target.ToArray();
                }
                if (goodset.File2 != null)
                {
                    MemoryStream target = new MemoryStream();
                    goodset.File2.InputStream.CopyTo(target);
                    bigImage = target.ToArray();
                }

                gs.ModifyGood(goodset.id, goodset.Name, image, bigImage, goodset.PriceDay, goodset.Latitude, goodset.Longitude);
                return RedirectToAction("MyStuff", siteName, null);
            }

            return View(goodset);
        }

    }
}